# coding: utf-8
from sqlalchemy import orm, create_engine, Table, MetaData, Column, String, Text, Integer
from sqlalchemy.exc import OperationalError

db = create_engine('sqlite:///databases/packages.db')

class Package(object):
    def __init__(self, name, version, description, dependency):
        self.name = name
        self.version = version
        self.description = description
        self.dependency = dependency

metadata = MetaData()

database_table = Table('packages', metadata,
    Column('id', Integer, primary_key=True),
	Column('name', String(24)),
	Column('version', String(8)),
	Column('description', Text()),
	Column('dependency', String(64))
    )



orm.mapper(Package, database_table)
session = orm.Session(bind=db)

print session.query(Package.name, Package.version).all()

# newPackage = Package("python", "2.7.2", "Python VM version 2.7.2", "libgc >= 1.4.4")

# session.add(newPackage)

# try:
#     session.commit()
# except OperationalError:
#     # Если нет базы данных, создаём её:
#     metadata.create_all(db)
#     print "A new Database databases/packages.db has been created!"
#     session.commit()